//
//  UserAccount.swift
//  WEIBO_SWIFT
//
//  Created by 岳挺 on 15/6/28.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding{
    ///接口获取授权后的取得的access token
    var access_token : String
    ///access_token有效时间
    var expires_In : NSTimeInterval
    ///过期时间
    var expiresDate : NSDate
    ///当前授权用户的uid
    var uid : String
    ///用户名称
    var name : String?
    ///用户头像地址 180 * 180大小
    var icon : String?
    
    init(dict : [String : AnyObject]){
        access_token = dict["access_token"] as! String
        expires_In = dict["expires_in"] as! NSTimeInterval
        uid = dict["uid"] as! String
        
        //计算过期日期
        expiresDate = NSDate(timeIntervalSinceNow: expires_In)
    }
    
//    func loadUserInfo
    
    static let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("account.plist")
    
    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeDouble(expires_In, forKey: "expires_in")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(icon, forKey: "icon")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
    ///保存用户信息
    func saveUserAccount (){
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath)
    }
    
    ///加载用户信息
    func loadUserInfo (finished : (account : UserAccount? , error : NSError?) -> ()){
        let params = ["access_token" : access_token , "uid" : uid]
        let urlStr = "2/users/show.json"
        NetWorkTool.sharedNetWorkTool().GET(urlStr, parameters: params, success: { (_, JSON) -> Void in
            let dict = JSON as! [String : AnyObject]
            self.name = dict["name"] as? String
            self.icon = dict["avatar_large"] as? String
            
            //保存用户信息
            self.saveUserAccount()
            finished(account: self,error: nil)
            }) { (_, error) -> Void in
                //AFN显示异常
                finished(account: nil,error: error)
        }
    }
    
    ///加载用户信息，第一次加载的时候为空
    class func loadUserAccount () ->UserAccount? {
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(UserAccount.accountPath) as? UserAccount {
            if account.expiresDate.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return account
            }
        }
        return nil
    }
    
    ///解档
    required init?(coder aDecoder: NSCoder) {
        expires_In = aDecoder.decodeDoubleForKey("expires_in")
        access_token = aDecoder.decodeObjectForKey("access_token") as! String
        uid = aDecoder.decodeObjectForKey("uid") as! String
        name = aDecoder.decodeObjectForKey("name") as? String
        icon = aDecoder.decodeObjectForKey("icon") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as! NSDate
    }
    
    override var description: String {
        let properties = ["access_token","expires_In","uid","name","icon"]
        return "\(self.dictionaryWithValuesForKeys(properties))"
    }
    
}
